/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Service
*        文件名:             ItemDetailsService.svc.cs
*        创建系统时间:       2012/11/2 19:07:05
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Service
{
    using System;
    using System.Data;
    using System.Reflection;
    using System.ServiceModel;
    using ECP.DataAccess;
    using ECP.DataAccess.Utilities;
    using ECP.Interface;
    using ECP.Model;
    using ECP.Utilities;

    [ServiceBehavior(Namespace = "http://jiangguang.net.cn/")]
    public partial class ItemDetailsService : MarshalByRefObject, IItemDetailsService
    {
        private static ItemDetailsService instance = null;
        private static object locker = new object();

        public string Add(BaseUserInfo userInfo, DataTable dataTable, out string statusCode, out string statusMessage)
        {
            string str = string.Empty;
            statusCode = string.Empty;
            statusMessage = string.Empty;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseItemDetailsDao dao = new BaseItemDetailsDao(dbHelper, userInfo, dataTable.TableName);
                BaseItemDetailsEntity itemDetailsEntity = new BaseItemDetailsEntity(dataTable);
                str = dao.Add(itemDetailsEntity, out statusCode);
                statusMessage = dao.GetStateMessage(statusCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return str;
        }

        public int BatchDelete(BaseUserInfo userInfo, string tableName, string[] ids)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseItemDetailsDao(dbHelper, userInfo, tableName).BatchDelete(ids);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int BatchMoveTo(BaseUserInfo userInfo, string tableName, string[] ids, string targetID)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseItemDetailsDao dao = new BaseItemDetailsDao(dbHelper, userInfo, tableName);
                for (int i = 0; i < ids.Length; i++)
                {
                    num += dao.SetProperty(ids[i], BaseItemDetailsTable.FieldParentID, targetID);
                }
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int BatchSave(BaseUserInfo userInfo, DataTable dataTable)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseItemDetailsDao(dbHelper, userInfo, dataTable.TableName).BatchSave(dataTable);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int Delete(BaseUserInfo userInfo, string tableName, string id)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseItemDetailsDao(dbHelper, userInfo, tableName).Delete(id);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public DataTable Get(BaseUserInfo userInfo, string tableName, string id)
        {
            DataTable table = new DataTable(tableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                table = new BaseItemDetailsDao(dbHelper, userInfo, tableName).Get(id);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public DataSet GetDSByCodes(BaseUserInfo userInfo, string[] codes)
        {
            DataSet set = new DataSet();
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                for (int i = 0; i < codes.Length; i++)
                {
                    DataTable table = this.GetDTByCode(dbHelper, userInfo, codes[i]);
                    table.TableName = codes[i];
                    set.Tables.Add(table);
                }
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return set;
        }

        public DataTable GetDT(BaseUserInfo userInfo, string tableName)
        {
            DataTable dT = new DataTable(tableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                dT = new BaseItemDetailsDao(dbHelper, userInfo, tableName).GetDT();
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dT;
        }

        public DataTable GetDTByCode(BaseUserInfo userInfo, string code)
        {
            DataTable table = new DataTable(BaseItemDetailsTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                table = this.GetDTByCode(dbHelper, userInfo, code);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public DataTable GetDTByCode(IDbHelper dbHelper, BaseUserInfo userInfo, string code)
        {
            DataTable table = new DataTable(BaseItemDetailsTable.TableName);
            BaseItemDetailsDao dao = new BaseItemDetailsDao(dbHelper, userInfo);
            BaseItemsDao dao2 = new BaseItemsDao(dbHelper, userInfo);
            BaseItemsEntity entity = new BaseItemsEntity(dao2.Get(BaseItemsTable.FieldCode, code));
            if (!string.IsNullOrEmpty(entity.TargetTable))
            {
                dao = new BaseItemDetailsDao(dbHelper, userInfo, entity.TargetTable);
            }
            return dao.GetDT();
        }

        public DataTable GetDTByParent(BaseUserInfo userInfo, string tableName, string parentID)
        {
            DataTable dTByParent = new DataTable(tableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                dTByParent = new BaseItemDetailsDao(dbHelper, userInfo, tableName).GetDTByParent(parentID);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dTByParent;
        }

        public void Load()
        {
        }

        public int Update(BaseUserInfo userInfo, DataTable dataTable, out string statusCode, out string statusMessage)
        {
            statusCode = string.Empty;
            statusMessage = string.Empty;
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseItemDetailsEntity itemDetailsEntity = new BaseItemDetailsEntity(dataTable);
                BaseItemDetailsDao dao = new BaseItemDetailsDao(dbHelper, userInfo, dataTable.TableName);
                num = dao.Update(itemDetailsEntity, out statusCode);
                statusMessage = dao.GetStateMessage(statusCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public static ItemDetailsService Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new ItemDetailsService();
                        }
                    }
                }
                return instance;
            }
        }
    }
}